﻿using System;
using System.ComponentModel;
using System.Windows;

namespace WpfAdvControls.Component
{
    public abstract class ComponentBase : IMinComponent
    {
        #region ILogSource Members

        public event EventHandler<LogEventArgs> OnLog;

        #endregion

        public virtual void OnSettingsView(Window owner)
        {
        }

        protected void Log(string message)
        {
            if (OnLog != null)
            {
                OnLog(this, new LogEventArgs {Message = message});
            }
        }

        protected void SetError(string message)
        {
            IsError = true;
            ErrorMessage = message;
        }

        protected void SetError(Exception ex)
        {
            SetError(ex.ToString());
        }

        protected void ClearError()
        {
            IsError = false;
            ErrorMessage = string.Empty;
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        protected void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion

        #region IErrorInfo Members

        private bool isError;

        public bool IsError
        {
            get { return isError; }
            set
            {
                isError = value;
                NotifyPropertyChanged("IsError");
            }
        }

        private string errorMessage;

        public string ErrorMessage
        {
            get { return errorMessage; }
            set
            {
                errorMessage = value;
                NotifyPropertyChanged("ErrorMessage");
            }
        }

        #endregion
    }
}